---@class Battle.BaseComponent
local BaseComponent = Battle.CreateClass("BaseComponent")
Battle.BaseComponent = BaseComponent
function BaseComponent:ctor()
    self.components = {}
    self._parent = nil
end
function BaseComponent:IsUpdate()
    return self.__isUpdate
end
function BaseComponent:RegUpdate(isUpdate)
    self.__isUpdate = isUpdate
end
function BaseComponent:BaseCreate(...)
    self:OnCreate()
end

function BaseComponent:BaseDestroy()
    if self.components then
        for k,v in pairs(self.components) do
            v:BaseDestroy()
        end
    end
    self.components = nil
    self:OnDestroy()
end

--region component
function BaseComponent:AddComponent(typeComponent)
    if not typeComponent then
        return nil
    end
    if self.components == nil then
        ---@type Battle.BaseComponent[]
        self.components = {}
    end
    local com = typeComponent.New()
    table.insert(self.components,com)
    com._parent = self
    com:BaseCreate(self)
    if self._isInitialized then
        self:BaseInitialized()
    end
    return com
end
function BaseComponent:GetComponent(type)
    if not self.components then return nil end
    for k,v in pairs(self.components) do
        if v:GetType() == type then
            return v
        end
    end
    return nil
end
function BaseComponent:RemoveComponent(type)
    if not self.components then return  end
    for k,v in pairs(self.components) do
        if v:GetType() == type then
            v:BaseDestroy()
            table.remove(self.components,k)
            break
        end
    end
end
function BaseComponent:BaseInitialized()
    self._isInitialized = true
    if self.components then
        for k,v in pairs(self.components) do
            v:BaseInitialized()
        end
    end
    self:OnInitialized()
end
function BaseComponent:BaseInitializedAfter()
    if self.components then
        for k,v in pairs(self.components) do
            v:BaseInitializedAfter()
        end
    end
    self:OnInitializedAfter()
end
function BaseComponent:GetType()
    return self.__cname
end
--endregion component
--region override

function BaseComponent:OnCreate()

end

function BaseComponent:OnInitialized()

end
function BaseComponent:OnInitializedAfter()

end
function BaseComponent:OnDestroy()

end
function BaseComponent:Update(dt)

end

--endregion override
